package com.lantosec.hmrp.modules.revisit.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.revisit.entity.RevisitConfig;
import com.lantosec.hmrp.modules.revisit.service.IRevisitConfigService;
import com.lantosec.hmrp.modules.revisit.vo.RevisitConfigVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 回访功能配置 控制器
 *
 * @author AutoCode
 * @since 2020-04-30
 */
@Api(value = "回访功能配置控制器", tags = "回访功能配置控制器")
@RestController
@RequestMapping("/revisit/config")
@Log4j2
public class RevisitConfigController extends BaseController {
    @Autowired
    private IRevisitConfigService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<RevisitConfigVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<RevisitConfigVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<RevisitConfig> page,
                                              @ApiParam(value = "部門ID") @RequestParam(name = "deptId",required = false) String deptId,
                                              @ApiParam(value = "是否要求医生回访") @RequestParam(name = "revisitDoctor",required = false) Integer revisitDoctor,
                                              @ApiParam(value = "是否要求护士回访") @RequestParam(name = "revisitNurse",required = false) Integer revisitNurse){
        PageVO<RevisitConfigVO> revisitConfigVOPageVO = service.getAll(page,deptId,revisitDoctor,revisitNurse);
        return Result.ok(revisitConfigVOPageVO);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<RevisitConfigVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @GetMapping("getDeptId/{deptId}")
    @ApiOperation(value = "查询部門是否已存在")
    public Result<Void> getDeptId(@ApiParam(value = "ID", required = true) @PathVariable(name="deptId") String deptId){
        Result result = service.getDeptId(deptId);
        return result;
    }


    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<Integer> create(@ApiParam(value = "回访功能配置VO对象", required = true) @RequestBody @Validated RevisitConfigVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        RevisitConfig entity = vo.convertToEntity();
        service.save(entity);
        return Result.ok(entity.getConfigId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "回访功能配置VO对象", required = true) @RequestBody RevisitConfigVO vo){
        RevisitConfig entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
